# from pypinyin import pinyin
filename_in = ''
filename_out = ''
province = []
str_list = input().split()
if len(str_list) >= 2:
    filename_in = str_list[0]
    filename_out = str_list[1]
    for i in str_list[1]:
        if i == '省':
            filename_out = ''
            for j in range(1, len(str_list)):
                province.append(str_list[j])


def Direct_Out():
    file_in = open('yq_in.txt', 'r')
    line2 = file_in.readlines()
    for L2 in line2:
        print(L2)
    file_in.close()


def No_Appoint_Province():
    file_nap_in = open(filename_in, 'r')
    file_nap_out = open(filename_out, 'w')
    line1 = file_nap_in.readlines()
    judge_different = ''
    for L1 in line1:
        list_cut1 = L1.strip().split("\t")
        if judge_different != list_cut1[0]:
            judge_different = list_cut1[0]
            file_nap_out.write('\n')
            file_nap_out.write(list_cut1[0])
            file_nap_out.write('\n')
        for out1 in range(1, 3):
            file_nap_out.write(list_cut1[out1])
            file_nap_out.write('\t')
        file_nap_out.write('\n')
    file_nap_in.close()
    file_nap_out.close()


def Appoint_Province():
    file_ap_in = open(filename_in, 'r')
    line = file_ap_in.readlines()
    all_sort = []
    province_amount = 0
    dic = dict()
    for i in province:
        for L in line:
            list_cut = L.strip().split("\t")
            if list_cut[0] == i:
                all_sort.append(list_cut)
                province_amount = province_amount + int(list_cut[2])
        dic[i] = province_amount
        dic1 = sorted(dic.items(), key=lambda x: x[1], reverse=True)

    n = len(all_sort)
    for j in range(0, n - 1):
        for i in range(0, n - 1 - j):
            if int(all_sort[i][2]) < int(all_sort[i + 1][2]):
                all_sort[i], all_sort[i + 1] = all_sort[i + 1], all_sort[i]

    # for i in range(n):
    #     pinyin_same = []
    #     dic_pinyin = dict()
    #     if all_sort[i][2] == all_sort[i+1][2]:
    #         pinyin_same.append(all_sort[i])
    #         pinyin_same.append(all_sort[i + 1])
    #         for j in range(1, n-i):
    #             if all_sort[i][2] == all_sort[i+j+1][2]:
    #                 pinyin_same.append(all_sort[i+j+1])
    #         for z in pinyin_same:
    #             for q in z[1]:
    #                 dic_pinyin[z[1]] = pinyin(q[1])
    #                 dic_pinyin1 = sorted(dic.items(), key=lambda x: x[1], reverse=False)
    #     for j in range(n-i):
    #         if all_sort[i+j][1][0] == dic_pinyin1[j][1]
    #             all_sort[i+j] ==

    if dic1 != '':
        for i in dic1:
            print(i[0], '\t', i[1])
            for j in all_sort:
                if j[0] == i[0]:
                    print(j[1], '\t', j[2])
            print('\n')
    file_ap_in.close()


if len(str_list) == 0:
    Direct_Out()
elif filename_in != '' and filename_out != '' and province == []:
    No_Appoint_Province()
elif filename_in != '' and filename_out == '' and province != []:
    Appoint_Province()
else:
    pass
